package top.kaoshanji.p1ch02d;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

/**
 * 代码清单 p1ch02d-01：使用MethodHandles操作数组的示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/3 16:42
 */
public class MethodHandlesExample01 {

    private static final Logger logger = LogManager.getLogger(MethodHandlesExample01.class);

    public static void main(String[] args) throws Throwable {
        logger.info("...代码清单 p1ch02d-01：使用MethodHandles操作数组的示例...");

        int[] array = new int[] {1, 2, 3, 4, 5};

        MethodHandle setter = MethodHandles.arrayElementSetter(int[].class);
        setter.invoke(array, 3, 6);

        MethodHandle getter = MethodHandles.arrayElementGetter(int[].class);
        int value = (int)getter.invoke(array, 3);

        System.out.println(value);

    }

}
